
package net.sea.setup.action.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import net.sea.commons.context.AppContext;
import net.sea.commons.util.ELUtils;
import net.sea.commons.util.ELUtils.EL;
import net.sea.commons.util.FileUtils;
import net.sea.setup.action.AbstractAction;
import net.sea.setup.action.constants.Constants;

/**
 * 文件复制操作类
 *
 * @author huachengwu
 */
public class CopyfileAction extends AbstractAction {

	/*
	 * 复制文件操作的安装方法
	 * 
	 * @see
	 * com.ec.setup.action.IAction#install(net.sea.commons.context.AppContext,
	 * java.util.Map)
	 */
	@Override
	public void install(AppContext context, Map<String, String> parMap) {
		logger.info(parMap.get(Constants.INSTALL_DESC_KEY));
		long startTime = System.currentTimeMillis();
		// 源文件地址
		String oriFile = parMap.get(Constants.KEY_ORI_FILE);
		List<EL> els = ELUtils.getVarName(oriFile);
		for (EL el : els) {
			String value = (String) context.getAttribute(el.getVarName());
			oriFile = oriFile.replace(el.getElExp(), value.replace("\\", "/"));
		}
		if (logger.isDebugEnabled()) {
			logger.debug("源地址：" + oriFile);
		}
		// 目标文件地址
		String destFile = parMap.get(Constants.KEY_DEST_FILE);
		els = ELUtils.getVarName(destFile);
		for (EL el : els) {
			String value = (String) context.getAttribute(el.getVarName());
			destFile = destFile
					.replace(el.getElExp(), value.replace("\\", "/"));
		}
		if (logger.isDebugEnabled()) {
			logger.debug("目标地址：" + destFile);
		}
		try {
			FileUtils.copy(oriFile, destFile);
		} catch (IOException e) {
			logger.error("文件复制失败", e);
		}
		long endTime = System.currentTimeMillis();
		if (logger.isDebugEnabled()) {
			logger.debug("操作执行时间：" + (endTime - startTime) / 1000.0 + "秒");
		}
	}

	/*
	 * 复制文件操作的卸载方法
	 * 
	 * @see
	 * com.ec.setup.action.IAction#uninstall(net.sea.commons.context.AppContext,
	 * java.util.Map)
	 */
	@Override
	public void uninstall(AppContext context, Map<String, String> parMap) {
		logger.info(parMap.get(Constants.UNINSTALL_DESC_KEY));
		// 目标文件地址
		String desFile = parMap.get(Constants.KEY_DEST_FILE);
		List<EL> els = ELUtils.getVarName(desFile);
		for (EL el : els) {
			desFile = desFile.replaceAll(el.getElExp(),
					parMap.get(el.getVarName()));
		}
		// 删除解压后的目录
		FileUtils.delete(new File(desFile));
	}

}

/*
 * 修改历史 $Log$
 */